package com.xm.zcf.action.UploadAction;

import com.opensymphony.xwork2.ActionSupport;
import com.xm.zcf.domain.category.Category;
import com.xm.zcf.service.category.CategoryService;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/3/17.
 */
@Controller
@Scope("prototype")
public class UploadAction extends ActionSupport {
    private String imageFileName;
    private File image;
    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imagefileName) {
        this.imageFileName = imagefileName;
    }


    @Resource
    private CategoryService categoryService;

    public void setImage(File image) {
        this.image = image;
    }

    public File getImage() {
        return image;
    }

    public String exe(){
        String realPath= ServletActionContext.getServletContext().getRealPath("/products/5");
        Category c=new Category();
        if(image!=null){
            File file=new File(new File(realPath),imageFileName);
            try{
                FileUtils.copyFile(image,file);
                this.addActionMessage("上传成功");
            }catch (IOException e){
                e.printStackTrace();
                this.addActionMessage("上传失败");
            }
        }
        c.setCname("/products/5/"+imageFileName);
        categoryService.save(c);
        return "success";
    }
}
